﻿using AnlianCommon.Common.Domains.UserPermissions;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Org.BouncyCastle.Asn1.Ocsp;

namespace AnlianCommon.API.Commands.Personnels
{
    public class PersonnelCreateHander : IRequestHandler<PersonnelCreateCommand, Result>
    {
        private readonly IBaseRepository<Personnel> _personnelRepository;
        private readonly IMapper _mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="personnelRepository"></param>
        /// <param name="mapper"></param>
        public PersonnelCreateHander(IBaseRepository<Personnel> personnelRepository, IMapper mapper)
        {
            _personnelRepository = personnelRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 添加人员信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(PersonnelCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                Personnel personnel =_mapper.Map<Personnel>(request);
                var list = await _personnelRepository.Add(personnel);
                if (list > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Msg = "添加失败";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
